
class Parse:

    def main(self, classify):
        entity = classify['args']
        questions_type = classify['question_types']
        entity_dict = self.entity_transform(entity)

        sqls = []
        for question in questions_type:
            sql_dict = {}
            sql_dict["qustion_type"] = question
            sql_dict["sql"] = []
            sql = []
            if question == 'disease_symptom':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'symptom_disease':
                sql = self.sql_transfer(question, entity_dict.get('symptom'))

            elif question == 'disease_cause':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'disease_acompany':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'disease_not_food':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'disease_do_food':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'food_not_disease':
                sql = self.sql_transfer(question, entity_dict.get('food'))

            elif question == 'food_do_disease':
                sql = self.sql_transfer(question, entity_dict.get('food'))

            elif question == 'disease_drug':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'drug_disease':
                sql = self.sql_transfer(question, entity_dict.get('drug'))

            elif question == 'disease_check':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'check_disease':
                sql = self.sql_transfer(question, entity_dict.get('check'))

            elif question == 'disease_prevent':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'disease_lasttime':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'disease_cureway':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'disease_cureprob':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'disease_easyget':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            elif question == 'disease_desc':
                sql = self.sql_transfer(question, entity_dict.get('disease'))

            if sql:
                sql_dict['sql'] = sql

                sqls.append(sql_dict)

        return sqls

    def sql_transfer(self, question_type, entities):
        # 查询语句
        sql = []
        # 查询疾病的原因
        if question_type == 'disease_cause':
            sql = ["MATCH (m:Diseases) where m.name = '{0}' return m.name, m.cause".format(i) for i in entities]

        # 查询疾病的防御措施
        elif question_type == 'disease_prevent':
            sql = ["MATCH (m:Diseases) where m.name = '{0}' return m.name, m.prevent".format(i) for i in entities]

        # 查询疾病的持续时间
        elif question_type == 'disease_lasttime':
            sql = ["MATCH (m:Diseases) where m.name = '{0}' return m.name, m.cure_lasttime".format(i) for i in entities]

        # 查询疾病的治愈概率
        elif question_type == 'disease_cureprob':
            sql = ["MATCH (m:Diseases) where m.name = '{0}' return m.name, m.cured_prob".format(i) for i in entities]

        # 查询疾病的治疗方式
        elif question_type == 'disease_cureway':
            sql = ["MATCH (m:Diseases) where m.name = '{0}' return m.name, m.cure_way".format(i) for i in entities]

        # 查询疾病的易发人群
        elif question_type == 'disease_easyget':
            sql = ["MATCH (m:Diseases) where m.name = '{0}' return m.name, m.easy_get".format(i) for i in entities]

        # 查询疾病的相关介绍
        elif question_type == 'disease_desc':
            sql = ["MATCH (m:Diseases) where m.name = '{0}' return m.name, m.desc".format(i) for i in entities]

        # 查询疾病有哪些症状
        elif question_type == 'disease_symptom':
            sql = [
                "MATCH (m:Diseases)-[r:has_symptoms]->(n:Symptoms) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        # 查询症状会导致哪些疾病
        elif question_type == 'symptom_disease':
            sql = [
                "MATCH (m:Diseases)-[r:has_symptoms]->(n:Symptoms) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        # 查询疾病的并发症
        elif question_type == 'disease_acompany':
            sql1 = [
                "MATCH (m:Diseases)-[r:acompany_with]->(n:Symptoms) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
            sql2 = [
                "MATCH (m:Diseases)-[r:acompany_with]->(n:Symptoms) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
            sql = sql1 + sql2
        # 查询疾病的忌口
        elif question_type == 'disease_not_food':
            sql = ["MATCH (m:Diseases)-[r:not_eat]->(n:Foods) where m.name = '{0}' return m.name, r.name, n.name".format(i)
                   for i in entities]

        # 查询疾病建议吃的东西
        elif question_type == 'disease_do_food':
            sql1 = [
                "MATCH (m:Diseases)-[r:do_eat]->(n:Foods) where m.name = '{0}' return m.name, r.name, n.name".format(i)
                for i in entities]
            sql2 = [
                "MATCH (m:Diseases)-[r:recomment_eat]->(n:Foods) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
            sql = sql1 + sql2

        # 已知忌口查疾病
        elif question_type == 'food_not_disease':
            sql = ["MATCH (m:Diseases)-[r:not_eat]->(n:Foods) where n.name = '{0}' return m.name, r.name, n.name".format(i)
                   for i in entities]

        # 已知推荐查疾病
        elif question_type == 'food_do_disease':
            sql1 = [
                "MATCH (m:Diseases)-[r:do_eat]->(n:Foods) where n.name = '{0}' return m.name, r.name, n.name".format(i)
                for i in entities]
            sql2 = [
                "MATCH (m:Diseases)-[r:recomment_eat]->(n:Foods) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
            sql = sql1 + sql2

        # 查询疾病常用药品－药品别名记得扩充
        elif question_type == 'disease_drug':
            sql1 = [
                "MATCH (m:Diseases)-[r:common_drug]->(n:Drugs) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
            sql2 = [
                "MATCH (m:Diseases)-[r:recommand_drug]->(n:Drugs) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
            sql = sql1 + sql2

        # 已知药品查询能够治疗的疾病
        elif question_type == 'drug_disease':
            sql1 = [
                "MATCH (m:Diseases)-[r:common_drug]->(n:Drugs) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
            sql2 = [
                "MATCH (m:Diseases)-[r:recommand_drug]->(n:Drugs) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]
            sql = sql1 + sql2
        # 查询疾病应该进行的检查
        elif question_type == 'disease_check':
            sql = [
                "MATCH (m:Diseases)-[r:need_check]->(n:Checks) where m.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        # 已知检查查询疾病
        elif question_type == 'check_disease':
            sql = [
                "MATCH (m:Diseases)-[r:need_check]->(n:Checks) where n.name = '{0}' return m.name, r.name, n.name".format(
                    i) for i in entities]

        return sql

    def entity_transform(self, entity):
        entity_dict = {}
        for args, types in entity.items():
            for type in types:
                if type in entity_dict:
                    entity_dict[type] = [args]
                else:
                    entity_dict[type] = []
                    entity_dict[type].append(args)
        return entity_dict

    def test(self):
        str = "hwllo {1}".format("world", "fc")
        print(str)

if __name__ == "__main__":
    parse = Parse()
    # parse.test()
    classify = {'args': {'职业黑变病': ['disease'], '倒睫': ['disease', 'symptom']}, 'question_types': ['disease_not_food']}
    res = parse.main(classify)
    print(res)
